home *** CD-ROM | disk | FTP | other *** search
- Path: fohnix.metronet.com!not-for-mail
- From: milam@fohnix.metronet.com (Stan Milam)
- Newsgroups: comp.lang.c
- Subject: Re: Easiest way to center a string?
- Date: 16 Feb 1996 16:23:01 -0600
- Organization: Texas Metronet, Inc (login info (214/488-2590 - 817/571-0400))
- Message-ID: <4g3045$k4@fohnix.metronet.com>
- References: <4fobka$1st@parlor.hiwaay.net>
- NNTP-Posting-Host: fohnix.metronet.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- foxworth@hiwaay.net wrote:
- : I'm learning C, and one of my assignments requires that the output strings be
- : centered on the screen. Is there a simple way of doing this that I have overlooked?
-
-
- /**********************************************************************/
- /* File Id: centrstr.c. */
- /* Author: Stan Milam. */
- /* Description: */
- /* The following is an example of how to center a string using */
- /* printf and sprintf. It is just an example. Send flames to */
- /* /dev/null. */
- /* */
- /**********************************************************************/
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #define LINEWIDTH 80
- #define INDENT(string) ((strlen(string) + LINEWIDTH) / 2)
-
- int main( void ) {
-
- char wrkbuf[100];
- char string[] = "This is a sample string.";
-
- printf( "%*s\n", INDENT(string), string );
- sprintf( wrkbuf, "%*s", INDENT(string), string );
- puts( wrkbuf );
- return EXIT_SUCCESS;
- }
-